<?php
require_once( '../system/smarty.inc' );
require_once( '../system/mdao/Class_M_COST_TVSP.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );
require_once( '../system/Class_ERROR.php' );
require_once( '../system/Class_PWD.php' );
require_once( '../system/login.inc.php' );

Class_PWD::valid_auth(AOKI_AUTH_COST,$login['staff_auth']);

$dao = new Class_M_COST_TVSP;

if( $_SERVER['REQUEST_METHOD'] == 'GET' ){
	$phase = 'input';

	if( isset($_GET['id']) && $_GET['id'] != '' ){
		$forms  = $dao->get( $_GET['id'] );
		$forms["id"] = $_GET['id'];
		$forms["tele_date_start"] = Class_PWD::int8_to_date($forms["tele_date_start"]) ;
		$forms["tele_date_end"] = Class_PWD::int8_to_date($forms["tele_date_end"]) ;
	
	}else{
		//$forms['auth'] = 0;
	}


}else{
	$forms = $_POST;
	if( $forms['mode'] == 'input' ){
		$err     = new Class_ERROR;
		$chk     = $dao->get_checks();
		
		foreach ($chk as $kcol => $chkval) {
			$err_mes[$kcol]  = $err->check( $forms[$kcol], $chkval );
		}
		
		
		if ( $err->clear ){
			$cols     = $dao->get_cols();
			foreach($cols as $kcol => $val) {
				$values[$kcol] = $forms[$kcol] ;
			}
			//
			$values["tele_date_start"]    = Class_PWD::date_to_int8 ($values['tele_date_start']) ;
			$values["tele_date_end"]      = Class_PWD::date_to_int8 ($values['tele_date_end']) ;
			
			//桁数チェック
			$maxlens = $dao->get_maxlens();
			foreach ( $maxlens as $k => $v ) {
				$err_mes[$k] =$err->check_size($values[$k], $v);
			}
		}
		
/*
		if ( $err->clear ){
			if( $forms['id'] == '' ){
				$where = sprintf(" pattern_name  = '%s'", $forms['pattern_name'] );
			}else{
				$where = sprintf(" pattern_name = '%s' AND pattern_cd  <> %s", $forms['pattern_name'], $forms['id'] );
			}
			if ( $dao->exits($where) ) {
				$err->clear = false;
				$err_mes['pattern_name'] = '<p class="error">このパターン名は重複しています</p>';
			}
		}
	*/	

		if( $err->clear ){
			if( $forms['id'] == '' ){
				if ($dao->add0($values)) {
					$forms['id'] = $id;
					$smarty->assign('message', '登録が完了しました。');
					
				} else {
					$smarty->assign('message', '登録が失敗しました。');
				}
			}else{
				
				if ($dao->edit( $forms['id'], $values )) {
					$smarty->assign('message', '更新が完了しました。');
				} else {
					$smarty->assign('message', '更新が失敗しました。');
				}
				$id = $forms['id'];
			}
			$phase = 'complete';
		}else{
			$phase = 'input';
		}
	}
}

if (empty($forms["year4"])) {
	$forms["year4"] = date('Y');
}

if ($login['staff_auth'] == "G-9" ) {
	$electrics = Class_M_OTHER::get_electric_array(PLEASE_SELECT_ARR, $login['autoid'] );
	$arrelc= array_keys($electrics);
	$forms["electric_id"] = $arrelc[0] ;
}else {
	$electrics = Class_M_OTHER::get_electric_array(PLEASE_SELECT_ARR);
}

if( isset( $forms ) ) $smarty->assign('forms', $forms);
if( isset( $err_mes ) ) $smarty->assign('err', $err_mes);

if ($phase != 'complete') {
	
}

$smarty->assign('year4',  Class_PWD::get_years());

$week= Class_M_OTHER::get_week_array( $forms["year4"], PLEASE_SELECT_ARR );
$smarty->assign('week', $week );


$smarty->assign('electrics', $electrics );

$maters =  Class_M_OTHER::get_mater_array($forms["electric_id"], $forms["year4"], PLEASE_SELECT_ARR ) ;
$smarty->assign('maters', $maters );

$smarty->assign('phase', $phase );
$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );

$smarty->display('mcost_tvsp_form.html');
?>